package com.dzk.cp.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import com.dzk.cp.c11x5.constant.C11x5Constants;

public class CpUtils {
	
	public static List<List<Integer>> permute(List<Integer> list, int n) {
		List<List<Integer>> resultList = new ArrayList<List<Integer>>();
		LinkedList<Integer> linkedList = new LinkedList<Integer>(list);
		for (LinkedList<Integer> resList : permute(linkedList, n)) {
			resultList.add(resList);
		}
		return resultList;
	}
	
	private static List<LinkedList<Integer>> permute(LinkedList<Integer> linkedList, int n) {
		List<LinkedList<Integer>> resultList = new ArrayList<LinkedList<Integer>>();
		if (n == 1) {
			for (Integer obj : linkedList) {
				LinkedList<Integer> elemList = new LinkedList<Integer>();
				elemList.add(obj);
				resultList.add(elemList);
			}
		} else {
			for (int i = 0; i < linkedList.size(); i++) {
				Integer delObj = linkedList.remove(i);
				List<LinkedList<Integer>> nextResultList = permute(linkedList, n - 1);
				for (LinkedList<Integer> elemList : nextResultList) {
					elemList.add(0, delObj);
					resultList.add(elemList);
				}
				linkedList.add(i, delObj);
			}
		}
		return resultList;
	}
	
	public static List<List<Integer>> combine(List<Integer> list, int n) {
		List<List<Integer>> resultList = new ArrayList<List<Integer>>();
		LinkedList<Integer> linkedList = new LinkedList<Integer>(list);
		for (LinkedList<Integer> resList : combine(linkedList, n)) {
			resultList.add(resList);
		}
		return resultList;
	}
	
	private static List<LinkedList<Integer>> combine(LinkedList<Integer> linkedList, int n) {
		List<LinkedList<Integer>> resultList = new ArrayList<LinkedList<Integer>>();
		if (n == 1) {
			for (Integer obj : linkedList) {
				LinkedList<Integer> elemList = new LinkedList<Integer>();
				elemList.add(obj);
				resultList.add(elemList);
			}
		} else if (linkedList.size() == n) {
			resultList.add(linkedList);
		} else {
			List<Integer> firstList = new ArrayList<Integer>(linkedList.subList(0, linkedList.size() - n + 1));
			for (int i = 0; i < firstList.size(); i++) {
				Integer delObj = linkedList.remove(i);
				LinkedList<Integer> nextLinkedList = new LinkedList<Integer>(linkedList.subList(i, linkedList.size()));
				List<LinkedList<Integer>> nextResultList = combine(nextLinkedList, n - 1);
				for (LinkedList<Integer> elemList : nextResultList) {
					elemList.add(0, delObj);
					resultList.add(elemList);
				}
				linkedList.add(i, delObj);
			}
		}
		return resultList;
	}
	
	public static void unsort(List<Integer> list) {
		List<Integer> linkedList = new LinkedList<Integer>(list);
		Random r = new Random();
		for (int i = 0; i < list.size(); i++) {
			int n = r.nextInt(linkedList.size());
			list.set(i, linkedList.get(n));
			linkedList.remove(n);
		}
	}
	
	public static Long getServerCurrentTimeSeconds() {
		String url = PropsUtils.getProperty(C11x5Constants.CP360_CURTIME_URL);
		try {
			return Long.valueOf(HttpUtils.requestGBK(url, null));
		} catch (Exception e) {
			e.printStackTrace();
			return 0L;
		}
	}
	
	public static String fore2zero(int a) {
		return (a < 10 ? "0" : "") + a;
	}
	
	public static String fore3zero(int a) {
		return (a < 10 ? "00" : (a < 100 ? "0" : "")) + a;
	}
	
	public static String foreNzero(int a, int len) {
		String s = a + "";
		while (s.length() < len) {
			s = "0" + s;
		}
		return s;
	}
	
	public static String[] splitp2(String str) {
		String[] arr = new String[str.length() / 2];
		for (int i = 0; i < arr.length; i++) {
			arr[i] = str.substring(i << 1, (i << 1) + 2);
		}
		return arr;
	}
	
	public static String[] splitpn(String str, int n) {
		String[] arr = new String[str.length() / n];
		for (int i = 0; i < arr.length; i++) {
			arr[i] = str.substring(i * n, i * n + n);
		}
		return arr;
	}
	
	public static int[] split2Ints(String str) {
		String[] sarr = str.split(" ");
		int[] iArr = new int[sarr.length];
		for (int i = 0; i < sarr.length; i++) {
			iArr[i] = Integer.parseInt(sarr[i]);
		}
		return iArr;
	}
	
	public static String toJson(Object obj) {
		if (obj == null) {
			return "";
		} else if (obj instanceof String) {
			return (String) obj;
		} else if (obj instanceof Object[]) {
			return JSONArray.fromObject(obj).toString();
		} else {
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.setAllowNonStringKeys(true);
			return JSONObject.fromObject(obj, jsonConfig).toString();
		}
	}
	
	public static void main(String[] args) {
		Long l = getServerCurrentTimeSeconds();
		System.out.println(l);
		
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("01 02", 2);
		map.put("ad", 4);
		map.put("01 02 11", 3);
		map.put(23, "drg");
		System.out.println(toJson(map));
		
		String[] objs = new String[] {"01 08", "dr-3", "er"};
		System.out.println(toJson(objs));
		
		String str = "0206070801";
		System.out.println(splitpn(str, 3).length);
		
		StringBuilder strBuilder = new StringBuilder();
		for (int i = 0; i < str.length() >> 1; i++) {
			strBuilder.append(" ").append(str.substring(i << 1, (i << 1) + 2));
		}
		System.out.println(strBuilder.substring(1));
	}

}
